﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;
using MxWeiXinPF.BLL;


namespace MxWeiXinPF.Web.admin.wxapp
{
    public partial class wxappCreateQRPic : Web.UI.ManagePage
    {
        wxapp_userList auBll = new wxapp_userList();

        public int auid = 0;
        public int chicun = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            auid = MyCommFun.RequestInt("auid");
            chicun = MyCommFun.RequestInt("chicun");
            if (!Page.IsPostBack)
            {
                ShowInfo(auid, chicun);
            }

        }


        #region 赋值操作=================================

        private void ShowInfo(int id,int chicun)
        {

            hidid.Value = id.ToString();
            DataSet ds = auBll.GetTotModel(id);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                //有数据
                string auqrCodeUrl = MyCommFun.ObjToStr(dr["auqrCodeUrl"]);

                if (auqrCodeUrl != "")
                {
                    imgQRPic.ImageUrl = auqrCodeUrl;
                    hlDown.Visible = true;
                    hlDown.NavigateUrl = auqrCodeUrl;
                }
                else
                {
                    hlDown.Visible = false;
                }

                //ddlQRCC.SelectedValue = chicun.ToString();


            }

        }

        #endregion

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

           

            try
            {
                string qrPic = "";

                  DataSet ds = auBll.GetTotModel(auid);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    string path =MyCommFun.ObjToStr( dr["pageUrl"]);
                    string storeCompanyId= MyCommFun.ObjToStr(dr["storeCompanyId"]);
                    path = path.Replace("{userid}", storeCompanyId);

                    //生成二维码：调用接口
                  
                     string jsonData = "{\"path\": \""+ path + "\"}";
                    Model.WebApiResultData qrpicApiRet = WebApiPostUtils.SendData("https://xcx.mecareinfo.com/51autoshop/wxaapp/qrcode", jsonData);
                    if (qrpicApiRet != null && qrpicApiRet.result != null && qrpicApiRet.result.Length > 0)
                    {
                        qrPic = PicBase64Helper.base64ToImages(qrpicApiRet.result);
                    }
                   
                }
                else
                {
                    AddAdminLog(MXEnums.ActionEnum.Build.ToString(), "程序数据出现异常，请联系管理员！，auid=" + auid); //记录日志
                    JscriptMsg("程序数据出现异常，请联系管理员！", "back");
                    return;
                }

                 

                if (qrPic != "")
                {

                    Model.wxapp_userList auModel = new Model.wxapp_userList();
                    auModel = auBll.GetModel(auid);
                    auModel.qrCodeUrl = qrPic;
                    bool ret = auBll.Update(auModel);
                    if (ret)
                    {
                        AddAdminLog(MXEnums.ActionEnum.Build.ToString(), "生成二维码生成，auid=" + auid); //记录日志

                        //2 成功跳转
                        Response.Redirect(Utils.CombUrlTxt("wxappCreateQRPic.aspx", "auid={0}", auid.ToString()));
                    }
                    else
                    {
                        JscriptMsg("生成二维码插入数据库出现异常，请重新生成！", "back");
                        return;
                    }

                }
                else
                {
                    JscriptMsg("调用生成二维码接口出现异常，请重新生成！", "back");
                    return;

                }

            }
            catch (Exception ex)
            {
                JscriptMsg("生成二维码出现异常，请重新生成！", "back");
                return;
            }

        }

        protected void ddlQRCC_SelectedIndexChanged(object sender, EventArgs e)
        {

            //chicun = MyCommFun.Str2Int(ddlQRCC.SelectedItem.Value);
            //if (chicun != 0)
            //{

            //    try
            //    {
            //        //生成二维码：调用接口
            //        string qrPic = "http://avatar.csdn.net/1/D/9/1_majian_1987.jpg";
            //        // string jsonData = "{\"path\": \"pages /home/home?shopId =73&companyId=73\", \"width\": 430}";
            //        //qrPic= WebApiPostUtils.SendData("https://xcx.mecareinfo.com/51autoshop/wxaapp/qrcode", jsonData);

            //        if (qrPic != "")
            //        {

            //            Model.wxapp_userList auModel = new Model.wxapp_userList();
            //            auModel = auBll.GetModel(auid);
            //            auModel.qrCodeUrl = qrPic;
            //            bool ret = auBll.Update(auModel);
            //            if (ret)
            //            {
            //                AddAdminLog(MXEnums.ActionEnum.Build.ToString(), "生成二维码生成，auid=" + auid); //记录日志

            //                //2 成功跳转
            //                Response.Redirect(Utils.CombUrlTxt("wxappCreateQRPic.aspx", "auid={0}&chicun={1}", auid.ToString(), ddlQRCC.SelectedItem.Value));
            //            }
            //            else
            //            {
            //                JscriptMsg("生成二维码插入数据库出现异常，请重新生成！", "back");
            //                return;
            //            }

            //        }
            //        else
            //        {
            //            JscriptMsg("调用生成二维码接口出现异常，请重新生成！", "back");
            //            return;

            //        }

            //    }
            //    catch (Exception ex)
            //    {
            //        JscriptMsg("生成二维码出现异常，请重新生成！", "back");
            //        return;
            //    }

            //}


        }

    }
}